package com.shinhye.elephantsafe.recevier;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

public class BootCompletedRecevier extends BroadcastReceiver {
	private final String TAG = "BootCompletedRecevier";
	
	@Override
	public void onReceive(Context context, Intent intent) {
		SharedPreferences sp = context.getSharedPreferences("Setting", Context.MODE_PRIVATE);
		if(!sp.getBoolean("RunGuard", false)) {
			Log.i(TAG, "未启动防盗功能，跳过检查SIM卡");
			return;
		}
		
		TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		
//		String saved_sim = sp.getString("SIM", "") + "assd"; //Debug用
		String saved_sim = sp.getString("SIM", "");
		String current_sim = tm.getSimSerialNumber();
		String safephone = sp.getString("safephone", "");
		if(TextUtils.isEmpty(safephone)) {
			Log.e(TAG, "开启了防盗，但是安全号码为空？不科学...");
			return;
		}
		
		if(!current_sim.equals(saved_sim)) {
			String text = "Your phone with GuardThref function set has changed its SIM card now, it possibly has been stolen, you can send a command messsage to remote operate that phone!";
			SmsManager.getDefault().sendTextMessage(safephone, null, text, null, null);
			Editor editor = sp.edit();
			editor.putBoolean("simchanged", true);
			editor.commit();
			
			Log.i(TAG, "SIM卡已被更换！");
		} else {
			Editor editor = sp.edit();
			editor.putBoolean("simchanged", false);
			editor.commit();
			Log.i(TAG, "系统启动，检查SIM卡相同");
		}
	}

}
